package com.lanyam.travel.base.utils;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 将数据转换为对应图形适用的数据格式
 */
public class DataStatisticsUtil {

    private static final String NAME = "name";
    private static final String COUNT = "count";

    // 柱状图
    private static final String BAR_CHART = "bar";
    // 饼状图
    private static final String PIE_CHART = "pie";
    // 折线图
    private static final String LINE_CHART = "line";

    /**
     * 将数据转换成对应的图形格式的数据
     * @param chartType 转换的图形类型
     * @param queryResult 查询的数据
     * @return
     */
    public static Map transform(String chartType, List<Map> queryResult) {
        Map result = new HashMap();
        switch (chartType) {
            case BAR_CHART:
                System.out.println("BAR_GRAPH");
                System.out.println(queryResult);
                List xData = new ArrayList();
                List yData = new ArrayList();
                for(Map data: queryResult) {
                    xData.add(data.get(NAME));
                    yData.add(data.get(COUNT));
                }
                result.put("xData", xData);
                result.put("yData", yData);
                //将源结果数据返回，方便需要者进行特殊处理
                result.put("sourceData", queryResult);
                break;
            case PIE_CHART:
                List nameList = new ArrayList();
                for(Map data: queryResult) {
                    nameList.add(data.get(NAME));
                    data.put("value", data.get("count"));
                }
                result.put("names", nameList);
                result.put("sourceData", queryResult);
                break;
            case LINE_CHART:
                //todo
                System.out.println("LINE_CHART");
                break;
        }
        return result;
    }
    /**
     * 比较两个日期是否相同，只比较年月日，忽略时分秒
     * @param d1
     * @param d2
     * @return
     */
    public static boolean sameDate(Date d1, Date d2) {
        LocalDate localDate1 = ZonedDateTime.ofInstant(d1.toInstant(), ZoneId.systemDefault()).toLocalDate();
        LocalDate localDate2 = ZonedDateTime.ofInstant(d2.toInstant(), ZoneId.systemDefault()).toLocalDate();
        return localDate1.isEqual(localDate2);
    }
}
